﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace NinjaToolbox.SubComponentModule
{
    public static class SubComponentFactory
    {
        public static SubComponentBase CreateSubComponent(SubComponentModelBase model)
        {
            var result = default(SubComponentBase);
            var attributes = model.GetType().GetCustomAttributes(typeof(TargetSubComponentAttribute), true);

            if (attributes != null && attributes.Length > 0)
            {
                var targetSubComponentType = (attributes[0] as TargetSubComponentAttribute).Type;
                var subComponent = Activator.CreateInstance(targetSubComponentType) as SubComponentBase;

                (subComponent as ISubComponent).ModelInitialization(model);

                result = subComponent;
            }

            return result;
        }

        public static SubComponentBase CreateSubComponent<TModel>() where TModel : SubComponentModelBase
        {
            return CreateSubComponent(typeof(TModel));
        }

        public static SubComponentBase CreateSubComponent(Type modelType)
        {
            var model = Activator.CreateInstance(modelType) as SubComponentModelBase;

            return CreateSubComponent(model);
        }
    }
}
